package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.CheckGroup;


import com.itheima.service.CheckGroupService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

/*
 *@AUTHOR:LIUCHAO;
 *@DATE: 2020/9/6 20:04
 */
@Controller
@ResponseBody
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
      try{
          checkGroupService.add(checkGroup,checkitemIds);
      }catch (Exception e){
          e.printStackTrace();
//          throw new BusinessException("抛出异常 能不惑嘛");
          return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
      }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try{
            checkGroupService.update(checkGroup,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

   @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId( Integer id){
        List<Integer> checkitemsIds=null;
       try {
        checkitemsIds=  checkGroupService.findCheckItemIdsByCheckGroupId(id);
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
       }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkitemsIds);
    }
    @RequestMapping("/delete")
    public Result delete( Integer id){
        System.out.println(id);
        try{
            checkGroupService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if(checkGroupList != null && checkGroupList.size() > 0){
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
